@echo OFF
REM ***********************************************************************************************************************
rem - Author: Mohamed Adardour
rem - Date: 16-11-2009
rem - 
rem - Prerequisites: .NET Framework 2.0, Visual Studio 2005/2008, Sourcesafe 6.0, Blat v2.6.2 (Free 3de party tool for email)
rem - This script performs the folowing tasks:
rem - 0. Delete sitecollection and restart services
rem - 1. 'Get Latest Version' from SourceSafe (http://msdn.microsoft.com/en-us/library/661w6e3d(VS.80).aspx)
rem - 2. Rebuild Solution
rem - 3. Install all wsp solutions
rem - 4. Create SiteCollections
rem - 5. Check for build en provisioning errors
rem - 6. Send email to developers if errors occurred

rem WARNING: Replace this text <Fill> with the correct value
REM ***********************************************************************************************************************

cls
SET SITE_URL="http://portal.ontwikkel.nl"
SET CODE_MAP="D:\Deploy\Code"
SET DEPLOY_MAP="D:\Deploy"
SET Installed_MAP="D:\Deploy\Installed"

echo *******************************************************
echo Deploying WSP solutions
echo *******************************************************
echo.
echo.

CHOICE /C YN /M "Rollout SP Solution"
if %errorlevel% == 2 GOTO END

rem ===================================================================
rem 	DELETE SITECOLLECTION AND RESTART SERVICES
rem ===================================================================
echo Deleting SiteCollection
stsadm -o deletesite -url %SITE_URL%

rem echo Stopping owstimer services...
net stop "Windows SharePoint Services Timer"

rem echo Excecuting iisreset stop...
iisreset /stop

if EXIST output.log del output.log


:VSS
rem ===================================================================
rem 	GET LATEST VERSION
rem ===================================================================
rmdir /S /Q %CODE_MAP%
mkdir %CODE_MAP%
echo.
echo.
echo.
echo Get Latest Version...
SET SSDIR=\\<Fill>
@echo n | S:\RICK2\win32\ss.exe get "$/<Fill>" -GL%CODE_MAP% -R -I -Y<Fill> -GWS >>output.log


rem ===================================================================
rem 	REBUILD SOLUTION
rem ===================================================================
echo.
echo.
echo.
echo Build Solution...

for /R %CODE_MAP% %%p IN (*.csproj) do (
	"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe" "%%p" /p:Configuration=Debug /t:rebuild >>output.log
)


:WSP
rem ===================================================================
rem  REMOVE INSTALLED WSP SOLUTIONS
rem ===================================================================
for %%s in (*.wsp) do del %%s
for %%s in (%Installed_MAP%\*.wsp) do copy "%%s" %DEPLOY_MAP% /y
echo.
echo.
echo.
echo Remove installed wsp solutions...
FOR /f "delims=" %%s IN (FarmScoped.txt) DO (
 
	echo.
	echo Remove %%s
	
	stsadm -o retractsolution -name %%s -immediate >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1

	stsadm -o deletesolution -name %%s >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1
 
	if exist %%s del %%s
)

for %%s in (*.wsp) do (

	echo.
	echo Remove %%s

	stsadm -o retractsolution -name %%s -allcontenturls -immediate >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1

	stsadm -o deletesolution -name %%s >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1
	
	if exist %%s del %%s
)


rem ===================================================================
rem 	INSTALL WSP SOLUTIONS
rem ===================================================================
echo.
echo.
echo.
echo Copy WSP files...
for /R %CODE_MAP% %%f in (*.wsp) do copy "%%f" %DEPLOY_MAP% /y

rmdir /S /Q %Installed_MAP%
mkdir %Installed_MAP%


echo.
echo.
echo.
echo Install Farm scoped solutions...
FOR /f "delims=" %%s IN (FarmScoped.txt) DO (
	
	if EXIST %%s (
		echo.
		echo Install %%s

		stsadm -o retractsolution -name %%s -immediate >>output.log 2>&1
		stsadm -o execadmsvcjobs >>output.log 2>&1

		stsadm -o deletesolution -name %%s >>output.log 2>&1
		stsadm -o execadmsvcjobs >>output.log 2>&1
		
		stsadm -o addsolution -filename %%s >>output.log 2>&1
		stsadm -o execadmsvcjobs >>output.log 2>&1
	
		stsadm -o deploysolution -name %%s -immediate -allowGacDeployment -force >>output.log 2>&1
		stsadm -o execadmsvcjobs >>output.log 2>&1

		if exist %%s move %%s Installed
	)
)

echo Install Webapplication scoped solutions...
for %%s in (*.wsp) do (

	echo.
	echo Install %%s

	stsadm -o retractsolution -name %%s -allcontenturls -immediate >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1

	stsadm -o deletesolution -name %%s >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1
	
	stsadm -o addsolution -filename %%s >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1

	stsadm -o deploysolution -name %%s -url %SITE_URL% -immediate -allowgacdeployment -allowCasPolicies -force >>output.log 2>&1
	stsadm -o execadmsvcjobs >>output.log 2>&1

	if exist %%s move %%s Installed
)

rem ===================================================================
rem 	START SERVICES
rem ===================================================================
echo.
echo.
echo.
echo Starting owstimer service...
net start "Windows SharePoint Services Timer"

echo Excecuting iisreset start...
iisreset /start


:CSITE
rem ===================================================================
rem 	CREATE SITECOLLECTION
rem ===================================================================
echo.
echo.
echo.
CHOICE /C YN /M "Create SiteCollection"
if %errorlevel% == 2 GOTO ERR

echo.
echo Creating SiteCollection...
stsadm -o createsite -url %SITE_URL% -sitetemplate "SITEDEFINITION#0" -title "DSS Portal" -ownerlogin "Administrator" -owneremail "norepy@hotmail.com" -description "A test for my wsp solutions" >>output.log 2>&1


:ERR
rem ===================================================================
rem 	CHECK ERRORS
rem ===================================================================
if exist errors.log del errors.log
FINDSTR /E /C:"-- FAILED." output.log >> errors.log

SET HasErrors=0
FOR /f "delims=" %%e IN (errors.log) DO SET HasErrors=1
if %HasErrors% == 1 (
	echo. >> errors.log
	echo. >> errors.log
	echo. >> errors.log
)
FINDSTR "Fout mislukt" output.log >> errors.log

FOR /f "delims=" %%e IN (errors.log) DO GOTO SENDMAIL

GOTO FIN


:SENDMAIL
rem ===================================================================
rem 	SEND MAIL
rem ===================================================================
echo.
echo.
echo.
echo Errors found!!!
echo.
echo Send Mail...
blat -attacht errors.log,output.log -s "Build of Provisioning errors (Sharepoint portal)" -body "Build of Provisioning errors zijn geconstateerd in de ingechecked code. Zie bijlage"  -t mohamedadardour@bistrodev.nl 
GOTO END


:FIN
echo.
echo.
echo.
echo Rollout Finished Successfully
echo.

:END
echo.
echo.
echo.

pause